<?php
header("Access-Control-Allow-Origin:*");
/*
 * 学生组信息获取接口
 * @author 何慕佳 2015-01-04
	接口形式：POST接口
	接口地址：URL+getUserContact.php?userId=1&roomId=1
	返回数据： 
	{
		"groups": [
		{
			"sendType": "broadcastChartGroup",//固定值
			"sessionName": "阿萨德",//组名
			"sessionId": "g1"//组编号
		},
		{
			"sendType": "broadcastChartGroup",
			"sessionName": "阿德",
			"sessionId": "g2"
		}
		],
		"customerService": [
		{
			"sendType": "sendChartUser",//固定值
			"sessionName": "感受到", //客服名称（含老师）
			"sessionId": 2 //客服用户编号uid
		},
		{
			"sendType": "sendChartUser",
			"sessionName": "感",
			"sessionId": 3
		}
		],
		"students": [
		{
			"sendType": "sendChartUser",//固定值
			"sessionName": "王企鹅",//学生姓名
			"sessionId": 4//uid
		},
		{
			"sendType": "sendChartUser",
			"sessionName": "方式对吧V刹",
			"sessionId": 5
		}
		]
	}
 *
 */

//require_once '../../framework/init.php';
require_once '../../dz_framework/init.php';
$return_array = $not_student =  array();
$csg_uid = isset($_REQUEST['user_id']) ? $_REQUEST['user_id']:0;
$csg_chid = isset($_REQUEST['roomid']) ? $_REQUEST['roomid']:0;

//ini_set("display_errors","On");
//error_reporting(E_ALL);
if($csg_uid && $csg_chid){
	//DEBUG 获取主讲 按课时号获取
	$sql_course_hour = "SELECT ch_teacher_id,ch_id,cm_id FROM ".DB::table("course_hour")." WHERE ch_id='".$csg_chid."' LIMIT 1";
	$sql_course_hour_result = DB::fetch_first($sql_course_hour);	
	if($sql_course_hour_result){
		$count_key = count($return_array['mainTeahcer']);
		$return_array['mainTeahcer']['sendType'] = "sendChartUser";
		//$return_array['mainTeahcer']['sessionName'] = ext::user_realname($sql_course_hour_result['ch_teacher_id']).'-'.ext::school_name_by_uid($sql_course_hour_result['ch_teacher_id']);
		$return_array['mainTeahcer']['sessionName'] = ext::user_realname($sql_course_hour_result['ch_teacher_id']);		
		$return_array['mainTeahcer']['sessionId'] = $sql_course_hour_result['ch_teacher_id'];
		//DEBUG 非老师识别
		$not_student[] = $sql_course_hour_result['ch_teacher_id'];
	}	
	//DEBUG 获取助教 按课时号获取
	/* if($sql_course_hour_result['assistants']){
		if(strpos($sql_course_hour_result['assistants'],',')){
			$assistants_array = explode(',',$sql_course_hour_result['assistants']);
		}else{
			$assistants_array[] = $sql_course_hour_result['assistants'];
		}
		foreach($assistants_array AS $key => $value){
            //DEBUG 判断助教是否存在 开始
            $sessionName='';
            $assistants_realname = ext::user_realname($value);
            if($assistants_realname){
                $sessionName=$assistants_realname;
                $school_name = ext::school_name_by_uid($value);
                if($school_name){
                    $sessionName=$sessionName.'-'.$school_name;
                }
                //DEBUG 判断助教是否存在 结束
                $count_key = count($return_array['customerService']);
                $return_array['customerService'][$count_key]['sendType'] = "sendChartUser";
                $return_array['customerService'][$count_key]['sessionName'] = $sessionName;
                $return_array['customerService'][$count_key]['sessionId'] = $value;
                //DEBUG 非老师识别
                $not_student[] = $value;
            }
		}
	} */
	//DEBUG 客服不显示主讲 开始
    //$count_key = count($return_array['customerService']);
	//$return_array['customerService'][$count_key]['sendType'] = "sendChartUser";
	//$return_array['customerService'][$count_key]['sessionName'] = ext::user_realname($sql_course_hour_result['teacher_id']).'-'.ext::school_name_by_uid($sql_course_hour_result['teacher_id']);
	//$return_array['customerService'][$count_key]['sessionId'] = $sql_course_hour_result['teacher_id'];
    //DEBUG 客服不显示主讲 结束
	//DEBUG 获取所有选课学生 按课程号获取
    /*
	$sql_group = "SELECT csg_gid FROM ".DB::table('course_student_group')." WHERE csg_chid = '".$csg_chid."' AND csg_uid='".$csg_uid."' LIMIT 1";
	$group_id = DB::result_first($sql_group);
	$sql_csg = "SELECT csg_uid FROM ".DB::table("course_student_group")." WHERE csg_chid='".$csg_chid."' AND csg_gid='".$group_id."'";
	$student_course_array = DB::fetch_all($sql_csg);

	//DEBUG 优化取出本组所有学生名称
	foreach($student_course_array AS $key => $value){
		$student_uid[$value['csg_uid']]=$value['csg_uid'];
	}
    */
    //DEBUG 修改为获取所有选择课时的学生
    $sql_student = "SELECT user_id AS csg_uid FROM ".DB::table('trade_course')." WHERE tc_flag = 2 AND cm_id ='".$sql_course_hour_result['cm_id']."'";
    $result_student = DB::fetch_all($sql_student);
    foreach($result_student AS $key => $value){
        $student_uid[$value['csg_uid']]=$value['csg_uid'];
    }
    //DEBUG 取出所有学校
    $sql = "SELECT sm_id as d_id,sm_name as d_name FROM ".DB::table('school_main');
    $dictionary_result = DB::fetch_all($sql);
    $dictionary_school=array();
    foreach($dictionary_result AS $key=>$value){
        $dictionary_school[$value['d_id']]=str_replace(array('绍兴市','柯桥区'),array('',''),$value['d_name']);
    }
	$all_users_name_sql = "SELECT user_id,user_realname,user_nickname,sm_id FROM ".DB::table("user_detail")." WHERE user_id IN (".dimplode($student_uid).")";
	$all_users_name_result = DB::fetch_all($all_users_name_sql);
	foreach($all_users_name_result AS $key => $value){
        $dept_name='';
        if($value['sm_id'] && $dictionary_school[$value['sm_id']]){
            $dept_name='-'.$dictionary_school[$value['sm_id']];
        }
		if($value['user_realname']){
			$all_users_name[$value['user_id']]=$value['user_realname'].$dept_name;
		}elseif($value['user_realname']){
			$all_users_name[$value['user_id']]=$value['user_realname'].$dept_name;
		}/* else{
			$all_users_name[$value['user_id']]=$value['user_name'].$dept_name;
		} */
	}
	if($student_uid){
        $group_id=1;
        $count_group_key = count($return_array['groups']);
        $return_array['groups'][$count_group_key]['sendType'] = "broadcastChartGroup";
        $return_array['groups'][$count_group_key]['sessionName'] = '第'.$group_id.'组';
        $return_array['groups'][$count_group_key]['sessionId'] = 'g'.$group_id;
		foreach($student_uid AS $key => $value){
			if($csg_uid==$value){

			}
			if(!in_array($value,$not_student) && ($csg_uid != $value)){
				$count_key = count($return_array['students']);
				$return_array['students'][$count_key]['sendType'] = "sendChartUser";
				$return_array['students'][$count_key]['sessionName'] = $all_users_name[$value];
				$return_array['students'][$count_key]['sessionId'] = $value;
			}
		}
		if(empty($return_array['groups'])){
			$return_array['students'] = $return_array['groups'] = array();
		}
        unset($return_array['groups']);
	}
}
echo json_encode($return_array);